package com.htj.setting.service.impl;

import com.htj.setting.dao.UserDao;
import com.htj.setting.domain.User;
import com.htj.setting.exception.UserException;
import com.htj.setting.service.UserService;
import com.htj.utils.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserDao dao;

    @Override
    public User login(String loginAct, String loginPwd,String ip) throws UserException {
        User user = dao.login(loginAct,loginPwd);
        if(user == null){
            throw new UserException("账号或密码错误");
        }
        String ips = user.getAllowIps();
        if(!ips.contains(ip)){
            throw new UserException("ip不可访问");
        }
        if("0".equals(user.getLockState())){
            throw new UserException("账号被锁定");
        }
        if(user.getExpireTime().compareTo(DateTimeUtil.getSysTime()) < 0){
            throw new UserException("账号已失效");
        }
        return user;
    }

    @Override
    public List<User> getUserList() {
        List<User> ulist = dao.getUserList();
        return ulist;
    }
}
